<template>
	<view class="main-box">
		<view class="navigator-bar" :style="style">
			<image class="back-img" src="/static/backArrow2.png" @click="back()"></image>
		</view>
		<text class="title">{{isLogin ? '手机号注册登录' : '手机号绑定'}}</text>
		<view class="input-box">
			<input class="phone-input" v-model="phone" placeholder="输入手机号码"/>
		</view>
		<view class="input-box" v-if="isPassword">
			<input class="password-input" v-model="password" password="ture" placeholder="输入密码"/>
		</view>
		<view class="input-box" v-if="!isPassword">
			<input class="code-input" placeholder="输入验证码"/>
			<text class="code-get">获取验证码</text>
		</view>
		<text class="login-type-title" @click="changeInput()">{{ isPassword ? '验证码登录' : '密码登录'}}</text>
		<view class="login-button" @click="commit()">登录</view>
		<view class="agree-box">
			<checkbox checked="true" color="#FF691F" style="transform:scale(0.6)"/>
			<text class="agree-text1">登录注册即同意</text>
			<text class="agree-text2">《BOOM用户协议》</text>
			<text class="agree-text2">、</text>
			<text class="agree-text2">《隐私政策》</text>
		</view>
	</view>
</template>

<script>
	import Vue from 'vue'
	
	export default {
		data() {
			return {
				isLogin: true,
				isPassword: true,
				style: 'margin-top: 0px;',
				phone: '',
				password: ''
			};
		},
		onLoad() {
			uni.getSystemInfo({
				success: (res) => {
					this.style = `margin-top: ${res.safeArea.top}px`;
				}
			}),
			this.initData();
		},
		methods: {
			initData() {
				uni.getStorage({
					key: 'phone',
					success: (res) => {
						this.phone = res.data;
					}
				})
				uni.getStorage({
					key: 'password',
					success: (res) => {
						this.password = res.data;
					}
				})
			},
			changeInput() {
				this.isPassword = !this.isPassword;
			},
			back() {
				uni.navigateBack();
			},
			commit() {
				if (this.phone === '') {
					uni.showToast({
						title: '请输入正确的手机号',
						icon:"none"
					});
					return;
				}
				if (this.isLogin) {
					if (this.isPassword) {
						if (this.password === '') {
							uni.showToast({
								title: '请输入密码',
								icon:"none"
							});
							return;
						}
						const params = {
							mobile: this.phone,
							password: this.password,
						}
						this.$apis.login(params).then((res) => {
							if (res.code === 0) {
								Vue.prototype.$userInfo = res.data;
								uni.setStorage({
									key: 'phone',
									data: this.phone
								});
								uni.setStorage({
									key: 'password',
									data: this.password
								});
								uni.setStorage({
									key: 'token',
									data: res.data.token
								})
								uni.redirectTo({
									url: '/pages/index'
								})
							}
						});
					} else {
						
					}
				} else {
					
				}
			}
		}
	}
</script>

<style lang="scss">
	.main-box {
		width: 100vw;
		height: 100vh;
		background-color: white;
		display: flex;
		flex-direction: column;
		align-items: center;
		
		.navigator-bar {
			width: 100vw;
			height: 86rpx;
			display: flex;
			justify-content: flex-start;
			align-items: center;
			
			.back-img {
				width: 24rpx;
				height: 40rpx;
				margin-left: 42rpx;
			}
		}
		
		.title {
			font-size: 46rpx;
			font-weight: 500;
			color: #222222;
			width: 566rpx;
			margin-bottom: 19rpx;
			margin-top: 60rpx;
		}
		
		.input-box {
			width: 504rpx;
			height: 96rpx;
			background: #F6F7F9;
			border-radius: 23rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;
			margin-top: 46rpx;
			padding: 0 31rpx 0 31rpx;
			
			.phone-input {
				
			}
			
			.code-get {
				font-size: 31rpx;
				font-weight: 400;
				color: #FF5E2E;
			}
		}
		
		.login-type-title {
			font-size: 27rpx;
			font-weight: 500;
			color: #222222;
			width: 566rpx;
			margin-top: 31rpx;
		}
		
		.login-button {
			width: 566rpx;
			height: 96rpx;
			background: #FF5E2E;
			border-radius: 23rpx;
			font-size: 31rpx;
			font-weight: bold;
			line-height: 96rpx;
			color: #FFFFFF;
			text-align: center;
			margin-top: 77rpx;
		}
		
		.agree-box {
			margin-top: 70rpx;
			display: flex;
			align-items: center;
			position: absolute;
			bottom: 96rpx;
			
			.agree-text1 {
				font-size: 23rpx;
				font-weight: 400;
				color: #999999;
			}
			
			.agree-text2 {
				font-size: 23rpx;
				font-weight: 500;
				color: #222222;
			}
		}
	}
</style>
